<script setup lang="ts" name="Menu">
import editPage from "./edit.vue";
import detailPage from "./detail.vue";
import { MENU_TYPE_COLUMN, MENU_TYPE_ENUM } from "@/views/system/menu/const";
import { delMenuApi, listMenuApi } from "@/api/system/menu";

const { proxy } = getCurrentInstance();

const { sys_is_not, sys_show_hide, sys_normal_disable } = useDict('sys_is_not', "sys_show_hide", "sys_normal_disable")

// 遮罩层
const loading = ref(false);

// 导出遮罩层
const exportLoading = ref(false);

// 搜索栏元素
const queryMenuRef = ref(null);

// 表单Id
const id = ref(null);

// 父级ID
const parentId = ref(null);

// 选中数组
const selection = ref<MenuRecord[]>([])

// 全部展开
const isExpandAll = ref(false);

// 表格刷新
const refreshTable = ref(true);

// 菜单管理表数据
const menuList = ref<MenuRecord[]>([]);

// 弹出层标题
const title = ref("");

// 编辑弹出层
const openEdit = ref(false);

// 详情弹出层
const openDetail = ref(false);

//总条数
const total = ref(0);

// 列信息
const columns = ref<TableColumnsRecord>({
  colNo: { label: "序号", visible: true },
  menuId: { label: "ID", visible: true },
  parentId: { label: "上级菜单", visible: true },
  menuType: { label: "菜单类型", visible: true },
  icon: { label: "图标", visible: true },
  menuName: { label: "菜单名称", visible: true },
  orderNum: { label: "显示排序", visible: true },
  isFrame: { label: "是否外链", visible: true },
  path: { label: "路由地址", visible: true },
  component: { label: "组件路径", visible: true },
  perms: { label: "权限字符", visible: true },
  query: { label: " 路由参数", visible: true },
  isCache: { label: " 是否缓存", visible: true },
  visible: { label: "显示状态", visible: true },
  status: { label: "菜单状态", visible: true },
  createTime: { label: "创建时间", visible: true },
})

// 查询参数
const queryParams = ref<MenuRecord>({
  menuId: null,
  parentId: null,
  menuType: null,
  icon: null,
  menuName: null,
  orderNum: null,
  isFrame: null,
  path: null,
  component: null,
  perms: null,
  query: null,
  isCache: null,
  visible: null,
  status: null,
  createTime: null,
});

/** 查询菜单管理列表 */
function getList() {
  loading.value = true;
  listMenuApi(queryParams.value).then(response => {
    menuList.value = handleTree(response.data, "menuId");
    loading.value = false;
  });
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  if (refresh) {
    getList();
  }
  openEdit.value = false;
  openDetail.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryMenuRef.value.resetFields()
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selectionList: MenuRecord[]) {
  selection.value = selectionList;
}

/** 新增按钮操作 */
function handleAdd(row: MenuRecord) {
  if (row.menuId) {
    parentId.value = row.menuId;
  } else {
    parentId.value = null;
  }
  id.value = null;
  openEdit.value = true;
  title.value = "新增菜单管理";
}

/** 修改按钮操作 */
function handleUpdate(row: MenuRecord) {
  id.value = row.menuId || selection.value.map(item => item.menuId);
  openEdit.value = true;
  title.value = "修改菜单管理";
}

/** 详情按钮操作 */
function handleDetails(row: MenuRecord) {
  id.value = row.menuId;
  openDetail.value = true;
  title.value = "菜单管理详情";
}

/** 删除按钮操作 */
function handleDelete(row: MenuRecord | any) {
  let ids = [];
  let menuNames = [];
  if (row.menuId) {
    ids = [row.menuId];
    menuNames = [row.menuName];
  } else {
    selection.value.forEach(item => {
      ids.push(item.menuId);
      menuNames.push(item.menuName);
    })
  }

  // @ts-ignore
  proxy.$modal.confirm('是否确认删除名称为"' + menuNames + '"的数据项?', "系统提示", {
    type: "warning",
  }).then(() => {
    return delMenuApi(ids);
  }).then(() => {
    getList();
    // @ts-ignore
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出操作按钮 */
function handleExport() {
  download('system/menu/export', {
    ...queryParams.value
  }, `菜单管理_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<template>
  <div class="app-container">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryMenuRef" :inline="true" label-width="68px">
        <el-form-item label="菜单名称" prop="menuName">
          <el-input
              v-model="queryParams.menuName"
              placeholder="请输入菜单名称"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
              v-model="queryParams.status"
              placeholder="请选择菜单状态"
              clearable
              @keyup.enter.native="handleQuery"
          >
            <el-option
                v-for="(item, index) in sys_normal_disable"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="item-search">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button type="warning" icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="back-item">
      <div class="table-top mb8">
        <div class="title title-left">
          菜单管理列表
        </div>
        <el-button
            type="info"
            plain
            icon="Sort"
            @click="toggleExpandAll"
        >展开/折叠
        </el-button>
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-has-permi="['system:menu:add']"
        >新增
        </el-button>
        <span class="table-top-line" />
        <right-toolbar @queryTable="getList" :columns="columns" />
      </div>
      <el-table
          v-if="refreshTable"
          v-loading="loading"
          :data="menuList"
          row-key="menuId"
          v-tablesticky
          :default-expand-all="isExpandAll"
          @selection-change="handleSelectionChange"
      >
        <el-table-column label="菜单名称" prop="menuName" width="200" v-if="columns.menuName.visible" />
        <el-table-column label="菜单类型" prop="menuType" align="center" v-if="columns.menuType.visible">
          <template #default="scope">
            <dict-tag :options="MENU_TYPE_COLUMN" :value="scope.row.menuType" />
          </template>
        </el-table-column>
        <el-table-column label="图标" prop="icon" align="center" v-if="columns.icon.visible">
          <template #default="scope">
            <icon-svg :iconName="scope.row.icon" />
          </template>
        </el-table-column>
        <el-table-column label="显示排序" prop="orderNum" align="center" v-if="columns.orderNum.visible" />
        <el-table-column label="是否外链" prop="isFrame" align="center" v-if="columns.isFrame.visible">
          <template #default="scope">
            <dict-tag :options="sys_is_not" :value="scope.row.isFrame" />
          </template>
        </el-table-column>

        <el-table-column label="路由地址" prop="path" align="center" min-width="140" show-overflow-tooltip
                         v-if="columns.path.visible" />
        <el-table-column label="权限字符" prop="perms" align="center" min-width="140" show-overflow-tooltip
                         v-if="columns.perms.visible" />
        <el-table-column label="是否缓存" prop="isCache" align="center" v-if="columns.isCache.visible">
          <template #default="scope">
            <dict-tag :options="sys_is_not" :value="scope.row.isCache" />
          </template>
        </el-table-column>
        <el-table-column label="显示状态" prop="visible" align="center" v-if="columns.visible.visible">
          <template #default="scope">
            <dict-tag :options="sys_show_hide" :value="scope.row.visible" />
          </template>
        </el-table-column>
        <el-table-column label="菜单状态" prop="status" align="center" v-if="columns.status.visible">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" align="center" min-width="160"
                         v-if="columns.createTime.visible" />
        <el-table-column
            label="操作"
            align="center"
            fixed="right"
            min-width="140"
            class-name="small-padding
            fixed-width"
        >
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="handleUpdate(scope.row)"
                  v-has-permi="['system:menu:edit']"
              />
            </el-tooltip>
            <el-tooltip content="新增" placement="top" v-if="scope.row.menuType != MENU_TYPE_ENUM.button">
              <el-button
                  link
                  type="primary"
                  icon="Plus"
                  @click="handleAdd(scope.row)"
                  v-hasPermi="['system:menu:add']"
              />
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Document"
                  @click="handleDetails(scope.row)"
                  v-has-permi="['system:menu:query']"
              />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                  class="text-danger"
                  v-has-permi="['system:menu:remove']"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <div class="line-magin" />
    </div>

    <!-- 添加或修改菜单管理对话框 -->
    <edit-page v-if="openEdit" :parent-id="parentId" :id="id" :title="title" @closeDialog="closeDialog" />
    <detail-page v-if="openDetail" :id="id" :title="title" @closeDialog="closeDialog" />
  </div>
</template>

<style lang="scss" scoped>

</style>